package com.example.money.test.HideToobar;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;

public class FooterBehavior extends CoordinatorLayout.Behavior<View> {
    private int directionChange;
    private String TAG = "FooterBehavior";

    public FooterBehavior(Context context, AttributeSet attrs) {
        super(context,attrs);
    }
/*
返回值表示这次滑动我们是否需要关心，这里表示关心的是Y轴方向上的滑动
 */
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull
            View child, @NonNull View directTargetChild, @NonNull View target, int axes) {
        return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0 ;
    }

    @Override
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target) {
        Log.e(TAG, "onStopNestedScroll: Stop has been used");

        child.animate().cancel();
        directionChange = 0;
        show(child);
    }

    /*
    child就是我们定义的参数Linear
    dy表示水平滑动的距离，向上为正值，向下为负值
    directionChange为累加值。
    如果这这个值大于Linear，并且Linear是显示状态，就隐藏它；反之如果小于0,且Linear是GONE的状态，就显示它

     */
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                                  @NonNull View target, int dx, int dy, @NonNull int[] consumed) {
//        if(dy > 0 && directionChange < 0 || dy < 0 && directionChange > 0){//开始往返方向滑动
//            child.animate().cancel();
//            directionChange = 0;
//        }

        directionChange += dy;
        if(directionChange > child.getHeight() && child.getVisibility() == View.VISIBLE){
            hide(child);
        }else if(directionChange < 0 && child.getVisibility() == View.GONE){
            show(child);
        }

    }



    private void show(final View child) {
        ViewPropertyAnimator animator = child.animate().translationY(0).
                setInterpolator(new FastOutSlowInInterpolator()).setDuration(200);
        animator.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                child.setVisibility(View.VISIBLE);
            }
        });
        animator.start();
    }

    private void hide(final View child) {
        ViewPropertyAnimator animator = child.animate().translationY(child.getHeight()).
                setInterpolator(new FastOutSlowInInterpolator()).setDuration(200);
        animator.setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                child.setVisibility(View.GONE);
            }
        });
        animator.start();
    }




}
